#include "playerRole.h"
#include "jsonUtils.h"
#include "fileUtils.h"
#include "movieService.h"

void PlayerRole::fromJson(const JsonValue& value)
{
	JsonUtils::loadLong(value, L"id", id);
	JsonUtils::loadStr(value, L"name", name);
	JsonUtils::loadStr(value, L"gender", gender);
	JsonUtils::loadStr(value, L"crossGender", crossGender);
	JsonUtils::loadStr(value, L"filePath", filePath);
}

void PlayerRole::toJson(JsonValue& value, JsonDoc::AllocatorType& allocator,bool toConfig) const
{
	value.AddMember(L"id", id, allocator);
	JsonUtils::addString(value, L"name", name, allocator);
	JsonUtils::addString(value, L"gender", gender, allocator);
	JsonUtils::addString(value, L"crossGender", crossGender, allocator);
	JsonUtils::addString(value, L"filePath",  toConfig ? filePath : FileUtils::url(movieId, ROLE, id), allocator);
	if (!toConfig) {
		Movie* movie = MovieService::inst().findMovie(movieId);
		if (movie) {
			std::list<PlayRoleBook*> books = movie->findRoleBooks(id);
			if (!books.empty()) {
				JsonValue arr(rapidjson::kArrayType);
				for (std::list<PlayRoleBook*>::const_iterator it = books.begin(); it != books.end(); ++it) {
					PlayRoleBook* const& p = *it;
					JsonValue item(rapidjson::kObjectType);
					p->toJson(item, allocator, toConfig);
					arr.PushBack(item, allocator);
				}
				value.AddMember(L"books", arr, allocator);
			}
		}
	}
}

const int64_t& PlayerRole::getId() const
{
	return id;
}

void PlayerRole::setId(const int64_t& id)
{
	this->id = id;
}

const String& PlayerRole::getFilePath() const
{
	return filePath;
}

void PlayerRole::setMovieId(const int64_t& movieId)
{
	this->movieId = movieId;
}
